package com.ksd.common.weather;

import org.json.JSONArray;
import org.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CityRegionDataFromAPI {
    // 高德地图API密钥
    private static final String API_KEY = "5a60d68090e740d7bb515ea75a85cda1";
    // 城市列表API地址
    private static final String CITY_LIST_URL = "https://restapi.amap.com/v3/config/district?keywords=中国&subdistrict=1&key=" + API_KEY;
    // 地区列表API地址
    private static final String REGION_LIST_URL = "https://restapi.amap.com/v3/config/district?keywords=&subdistrict=2&key=" + API_KEY;

    // 获取所有城市
    public static List<String> getCities() {
        List<String> cities = new ArrayList<>();
        try {
            // 创建HttpClient对象
            HttpClient httpClient = HttpClientBuilder.create().build();
            // 创建HttpGet请求
            HttpGet request = new HttpGet(CITY_LIST_URL);
            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(request);
            // 解析响应数据
            String jsonString = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray districts = jsonObject.getJSONArray("districts");
            for (int i = 0; i < districts.length(); i++) {
                JSONObject cityObj = districts.getJSONObject(i);
                cities.add(cityObj.getString("name"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cities;
    }

    // 根据城市名称获取对应的地区
    public static List<String> getRegionsByCity(String cityName) {
        List<String> regions = new ArrayList<>();
        try {
            // 创建HttpClient对象
            HttpClient httpClient = HttpClientBuilder.create().build();
            // 构建地区列表API地址，替换关键词为城市名称
            String url = REGION_LIST_URL.replace("keywords=", "keywords=" + cityName);
            // 创建HttpGet请求
            HttpGet request = new HttpGet(url);
            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(request);
            // 解析响应数据
            String jsonString = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray districts = jsonObject.getJSONArray("districts");
            if (districts.length() > 0) {
                JSONObject cityObj = districts.getJSONObject(0);
                JSONArray children = cityObj.getJSONArray("districts");
                for (int i = 0; i < children.length(); i++) {
                    JSONObject regionObj = children.getJSONObject(i);
                    regions.add(regionObj.getString("name"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return regions;
    }

}